package com.wtone.platform.gat.images.gat.config;


import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.Duration;

/**
 * @Author: aiLin
 * @Date: 2020/9/4 15:39
 * @desc: redis多数据源配置
 */
@Configuration
public class RedisConfiguration {

    @Value("${redis.database.db1}")
    private int db1Database;

    @Value("${redis.database.db2}")
    private int db2Database;

    @Value("${redis.database.db3}")
    private int db3Database;

    @Value("${redis.database.db4}")
    private int db4Database;

    @Value("${redis.database.db5}")
    private int db5Database;

    @Value("${redis.database.db6}")
    private int db6Database;

    @Value("${redis.database.db7}")
    private int db7Database;

    @Value("${redis.database.db8}")
    private int db8Database;

    @Value("${redis.database.db9}")
    private int db9Database;

    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private int port;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.timeout}")
    private int timeout;

    @Value("${redis.pool.max-active}")
    private int maxActive;

    @Value("${redis.pool.max-idle}")
    private int maxIdle;

    @Value("${redis.pool.min-idle}")
    private int minIdle;

    @Value("${redis.pool.max-wait}")
    private int maxWait;

    @Bean
    public GenericObjectPoolConfig getPoolConfig(){
        // 配置redis连接池
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxWaitMillis(maxWait);
        return poolConfig;
    }


    @Bean(name = "db1RedisTemplate")
    public StringRedisTemplate getDb1RedisTemplate(){
        return getStringRedisTemplate(db1Database);
    }

    @Bean(name = "db2RedisTemplate")
    public StringRedisTemplate getDb2RedisTemplate(){
        return getStringRedisTemplate(db2Database);
    }

    @Bean(name = "db3RedisTemplate")
    public StringRedisTemplate getDb3RedisTemplate(){
        return getStringRedisTemplate(db3Database);
    }

    @Bean(name = "db4RedisTemplate")
    public StringRedisTemplate getDb4RedisTemplate(){
        return getStringRedisTemplate(db4Database);
    }

    @Bean(name = "db5RedisTemplate")
    public StringRedisTemplate getDb5RedisTemplate(){
        return getStringRedisTemplate(db5Database);
    }

    @Bean(name = "db6RedisTemplate")
    public StringRedisTemplate getDb6RedisTemplate(){
        return getStringRedisTemplate(db6Database);
    }

    @Bean(name = "db7RedisTemplate")
    public StringRedisTemplate getDb7RedisTemplate(){
        return getStringRedisTemplate(db7Database);
    }

    @Bean(name = "db8RedisTemplate")
    public StringRedisTemplate getDb8RedisTemplate(){
        return getStringRedisTemplate(db8Database);
    }

    @Bean(name = "db9RedisTemplate")
    public StringRedisTemplate getDb9RedisTemplate(){
        return getStringRedisTemplate(db9Database);
    }

    private StringRedisTemplate getStringRedisTemplate(int database) {
        // 构建工厂对象
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(host);
        configuration.setPort(port);
        configuration.setPassword(RedisPassword.of(password));
        LettucePoolingClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofSeconds(timeout)).poolConfig(getPoolConfig()).build();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, clientConfiguration);
        // 设置使用的redis数据库
        factory.setDatabase(database);
        // 重新初始化工厂
        factory.afterPropertiesSet();

        return new StringRedisTemplate(factory);
    }


}

